package com.jackstraw.jack_base.mq.rocketmq.bean;

import com.jackstraw.jack_base.mq.common.entity.MQRoutingTemplate;
import com.jackstraw.jack_base.util.ObjectUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;

import java.util.Objects;

/**
 * rocketMq路由类
 */
@Data
@Slf4j
public class RocketMQRoutingTemplate
        extends MQRoutingTemplate<RocketMQTemplateS,
        RocketMqProperties,
        RocketMqEntity,
        RocketMqProducer,
        RocketMqConsumer> {

    @Override
    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();

        this.getResolvedMQTemplateSMap().values().forEach(rocketMQTemplateS -> {
            DefaultMQProducer defaultMQProducer = rocketMQTemplateS.getDefaultMQProducer();
            if (ObjectUtil.ObjectUtilByLang3.isNotEmpty(defaultMQProducer)) {
                try {
                    defaultMQProducer.start();
                } catch (MQClientException e) {
                    log.error("Failed to startup Producer for RocketMQRoutingTemplate", e);

                }
            }
        });


    }
}
